package com.alen.sun.controller;

import com.alen.sun.bean.Package;
import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Result;
import com.alen.sun.service.ActivityService;
import com.alen.sun.service.PackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/packages")
@RestController
public class PackageController {
    @Autowired
    private PackageService packageService;

    /**
     * 添加套餐
     *
     * @param package1
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result addPackage(@RequestBody Package package1) {
        Boolean flag = packageService.addPackage(package1);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("添加套餐失败");
        }
    }

    /**
     * 修改套餐
     *
     * @param package1
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result updatePackage(@RequestBody Package package1) {
        Boolean flag = packageService.updatePackage(package1);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("修改套餐失败");
        }
    }

    /**
     * 删除套餐
     *
     * @param package1
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deletePackage(@RequestBody Package package1) {
        Boolean flag = packageService.deletePackage(package1);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("删除套餐失败");
        }
    }

    /**
     * 分类查询
     *
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPage")
    @ResponseBody
    public Result page(@RequestBody Map<String, Integer> requestParams) {
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = packageService.page(page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = packageService.deleteByIds(ids);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("批量删除失败");
        }

    }


}
